package io.github.ali.commons.variable;

import org.quartz.CronScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;

import io.github.ali.commons.function.NAFunction;


/**
 * CronTrigger实现
 * 
 * @author fuli
 * @date 2018年12月4日
 * @version 1.0.0
 */
public class CronActionGenerator extends QuartzActionGenerator {
	public static final String DEFAULT_CRON_EXPRESSION="* * * * * ? *";
	private String cronExpression;
	/**
	 * 每秒执行无限次
	 * @param action
	 */
	public CronActionGenerator(NAFunction action) {
		super(action);
		cronExpression = DEFAULT_CRON_EXPRESSION;
	}
	/**
	 * 按cron表达式执行
	 * @param cronExpression
	 * @param action
	 */
	public CronActionGenerator(String cronExpression,NAFunction action) {
		super(action);
		this.cronExpression=cronExpression;
	}
	@Override
	protected Trigger buildTrigger() {
		return TriggerBuilder.newTrigger().withIdentity(name)
				.withSchedule(
						CronScheduleBuilder.cronSchedule(cronExpression).withMisfireHandlingInstructionIgnoreMisfires())
				.build();
	}
}
